﻿using StudentCouncil.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StudentCouncil.DAL.DAO
{
    public class UserDAO
    {
        public static User Login(string uid, string pwd)
        {
            using (StudentContext db = new StudentContext())
            {
                var res = db.User
                    .Where(t => t.StudentId == uid && t.Password == pwd)
                    .FirstOrDefault();
                return res;
            }
        }

        /// <summary>
        /// 更新用户信息，但不包含头像，头像单独进行更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static bool UpdateInfo(User entity)
        {
            using (StudentContext db = new StudentContext())
            {
                var dbEntity = db.User.Find(entity.UserId);
                if (null == dbEntity)
                {
                    //Log4Helper.InfoLog.Warn("未找到该user id：" + entity.ID);
                    return false;
                }
                else
                {
                    dbEntity.Name = entity.Name;
                    dbEntity.StudentId = entity.StudentId;
                    //dbEntity.BirthDate = entity.BirthDate;
                    dbEntity.Gender = entity.Gender;

                    // 用户角色和头像需要单独进行设置，TODO
                    //dbEntity.Roles = entity.Roles;
                    //dbEntity.HeadImg = entity.HeadImg;

                    db.SaveChanges();
                    return true;
                }
            }
        }

        /// <summary>
        ///  更新用户头像
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static bool UpdateImage(User entity)
        {
            using (StudentContext db = new StudentContext())
            {
                var dbEntity = db.User.Find(entity.UserId);
                dbEntity.HeadImg = entity.HeadImg;

                db.SaveChanges();
                return true;
            }
        }

        public static User Getimgurl(int id) {

            using (StudentContext db = new StudentContext())
            {
                var dbEntity = db.User.Find(id);
                return dbEntity;
                
            }
        }
    }
}
